<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/6
 * Time: 10:43
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Other extends Base
{
    /**
     * 用户协议
     * @return mixed|\think\response\Json
     */
    public function userAgreement(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 1)
                ->update([
                    'other_user_agreement' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 1)->value('other_user_agreement');
        return $this->fetch('other/useragreement', ['agreement' => $agreement]);
    }
    
    /**
     * 隐私协议
     * @return mixed|\think\response\Json
     */
    public function ysAgreement(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 2)
                ->update([
                    'other_ys_agreement' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 2)->value('other_ys_agreement');
        return $this->fetch('other/ysagreement', ['agreement' => $agreement]);
    }
    
    /**
     * 法律声明
     * @return mixed|\think\response\Json
     */
    public function falv(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 3)
                ->update([
                    'other_falv' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 3)->value('other_falv');
        return $this->fetch('other/falv', ['agreement' => $agreement]);
    }
    
    /**
     * 服务相关
     * @return mixed|\think\response\Json
     */
    public function kefu(){
        if(Request::instance()->isPost()){
            $phone = input('phone');
            $email = input('email');
            $link = input('link');
            if(isset($email) && $email != ''){
                if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                    return json(['code' => 100, 'msg' => '请输入正确的邮箱格式']);
                }
            }
            if(isset($link) && $link != ''){
                $pattern="/^(http|https):\/\/.*$/i";
                if(!preg_match($pattern, $link)){
                    return json(['code' => 100, 'msg' => '请输入正确的网址格式']);
                }
            }
            $res = Db::table('up_other')
                ->where('other_id', 4)
                ->update([
                    'other_phone' => $phone,
                    'other_email' => $email,
                    'other_link' => $link,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $kefu = Db::table('up_other')
            ->where('other_id', 4)
            ->field('other_phone, other_email, other_link')
            ->find();
        return $this->fetch('other/kefu', ['kefu' => $kefu]);
    }
    
    /**
     * 平台协议
     * @return mixed|\think\response\Json
     */
    public function ptAgreement(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 5)
                ->update([
                    'other_pt_agreement' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 5)->value('other_pt_agreement');
        return $this->fetch('other/ptagreement', ['agreement' => $agreement]);
    }
    
    /**
     * 功能介绍
     * @return mixed|\think\response\Json
     */
    public function gnDesc(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 6)
                ->update([
                    'other_gn_desc' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 6)->value('other_gn_desc');
        return $this->fetch('other/gndesc', ['agreement' => $agreement]);
    }
    
    /**
     * 联系我们
     * @return mixed|\think\response\Json
     */
    public function callUs(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 7)
                ->update([
                    'other_call_us' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 7)->value('other_call_us');
        return $this->fetch('other/callus', ['agreement' => $agreement]);
    }
    
    /**
     * 主营商品可选最大数量
     * @return mixed|\think\response\Json
     */
    public function goodsChooseMaxNum(){
        if(Request::instance()->isPost()){
            $num = input('num');
            $res = Db::table('up_other')
                ->where('other_id', 8)
                ->update([
                    'other_goods_num' => $num,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $data = Db::table('up_other')->where('other_id', 8)->value('other_goods_num');
        return $this->fetch('other/goodschoosemaxnum', ['data' => $data]);
    }
    
    /**
     * 高级画师请愿达成人数
     * @return mixed|\think\response\Json
     */
    public function upHighPainterNum(){
        if(Request::instance()->isPost()){
            $num = input('num');
            $res = Db::table('up_other')
                ->where('other_id', 8)
                ->update([
                    'other_up_num' => $num,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $data = Db::table('up_other')->where('other_id', 8)->value('other_up_num');
        return $this->fetch('other/uphighpainternum', ['data' => $data]);
    }
    
    /**
     * 竞价规则
     * @return mixed|\think\response\Json
     */
    public function jingRole(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 9)
                ->update([
                    'other_jing_rule' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 9)->value('other_jing_rule');
        return $this->fetch('other/jingrole', ['agreement' => $agreement]);
    }
    
    /**
     * 竞价协议
     * @return mixed|\think\response\Json
     */
    public function jingAgreement(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $res = Db::table('up_other')
                ->where('other_id', 10)
                ->update([
                    'other_jing_agreement' => $content,
                    'other_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $agreement = Db::table('up_other')->where('other_id', 10)->value('other_jing_agreement');
        return $this->fetch('other/jingagreement', ['agreement' => $agreement]);
    }
    
    /**
     * 个人主页占位图
     * @return mixed|\think\response\Json
     */
    public function memberImage(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $update['other_updatetime'] = date('Y-m-d H:i:s');
            if(!empty($content)){
                $update['other_zhanwei_image'] = $content;
            }
            $res = Db::table('up_other')->where('other_id', 11)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $image = Db::table('up_other')->where('other_id', 11)->value('other_zhanwei_image');
        return $this->fetch('other/memberimage', ['image' => $image]);
    }
    
    /**
     * 集市缩略图
     * @return mixed|\think\response\Json
     */
    public function jishiImage(){
        if(Request::instance()->isPost()){
            $content = input('content');
            $update['other_updatetime'] = date('Y-m-d H:i:s');
            if(!empty($content)){
                $update['other_zhanwei_image'] = $content;
            }
            $res = Db::table('up_other')->where('other_id', 12)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $image = Db::table('up_other')->where('other_id', 12)->value('other_zhanwei_image');
        return $this->fetch('other/jishiimage', ['image' => $image]);
    }
}